package com.controller;

import com.common.ControllerSupport;
import com.model.BuildModelInfo;
import com.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @author: cxt
 * @time: 2022/7/11
 * 处理模板逻辑
 */
@RestController
@RequestMapping("/temp/create")
@Slf4j
public class TemplateController extends ControllerSupport {

    private TemplateService templateService;

    @PostMapping("/preview")
    public Map<String, String> createCodePreview(@RequestBody @Valid BuildModelInfo modelInfo, BindingResult bindingResult) {
        long l = System.currentTimeMillis();
        validErrorHandler(bindingResult);
        Map<String, String> resultMap = templateService.createCodePreview(modelInfo);
        templateService.saveByDisk(resultMap, modelInfo);
        long end = System.currentTimeMillis();
        log.info("生成模板耗时：{}", end - l);
        return resultMap;
    }

    @Autowired
    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }
}
